package com.example.pre.business.impl;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.pre.business.ReimbursementBusiness;
import com.example.pre.enums.BusinessStatusEnum;
import com.example.pre.enums.ReimbursementTypeEnum;
import com.example.pre.model.BillingData;
import com.example.pre.model.Reimbursement;
import com.example.pre.model.vo.ReimbursementVO;
import com.example.pre.service.BillingDataService;
import com.example.pre.service.ReimbursementService;
import com.example.pre.tools.JsonConvert;

@Service
@Transactional(rollbackFor = Exception.class)
public class ReimbursementBusinessImpl implements ReimbursementBusiness {

	@Autowired
	private ReimbursementService reimbursementService;
	@Autowired
	private BillingDataService billingDataService;

	@Override
	public ReimbursementVO createReimbursement(int userId, ReimbursementTypeEnum type, String description,
			Set<String> billingDataList) throws Exception {

		Reimbursement reimbursement = new Reimbursement();
		reimbursement.setCreateTime(System.currentTimeMillis());
		reimbursement.setCreateUserId(userId);
		reimbursement.setDescription(description);
		reimbursement.setStatus(BusinessStatusEnum.editing.name());
		reimbursement.setType(type.name());
		reimbursement.setUpdateTime(System.currentTimeMillis());
		reimbursement.setUpdateUserId(userId);
		reimbursementService.create(reimbursement);

		Collection<BillingData> billingDatas = JsonConvert.toBeans(billingDataList, BillingData.class);
		for (BillingData billingData : billingDatas) {
			billingData.setReimbursementId(reimbursement.getId());
		}
		// 批量创建报销明细
		billingDataService.createBatch(billingDatas);
		ReimbursementVO reimbursementVO = new ReimbursementVO(reimbursement, billingDatas);
		return reimbursementVO;
	}

	@Override
	public ReimbursementVO updateReimbursement(int userId, int reimbursementId, ReimbursementTypeEnum type,
			String description, Set<String> billingDataList) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ReimbursementVO checkReimbursement(int reimbursementId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> flowReimbursement(int reimbursementId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
